home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
138_01
/
oct84col.ddj
< prev
next >
Wrap
Text File
|
1985-11-13
|
18KB
|
581 lines
.pl 63
.po 0
..
.. this article was prepared for the C/Unix Programmer's Notebook
.. Copyright 1984 (c) Anthony Skjellum.
..
.. by A. Skjellum
..
.he "C/Unix Programmer's Notebook" for October, 1984 DDJ.
Introduction
The thrust of this entire column is to illustrate scientific uses
of the C programming language by examples. The aim is to present routines
useful in conjunction with scientific programming, some of which are
general and others which implement specific numerical algorithms. There are
specific audiences for which this column is intended. The first audience
is the group of die-hard programmers who refuse to change languages and
continue to produce useful programs in outdated languages. Not only is
this of greater expense to themselves, but also cheats others by locking
them into FORTRAN or other old-fashioned languages. For them, I want to
illustrate the elegance and versatility of C. The second audience (which
may also include the first as a subset) are those users interested in
scientific applications but who may not have used C for this purpose. This
article is intended to illustrate that C is completely acceptable for such
purposes and the code shows how generally concepts can be presented. (This
article makes no attempt to survey scientific applications where C could be
used but merely includes some non-trivial examples). Finally, for those
readers who don't fit into the above categories, the general purpose
routines will still prove interesting.
Three programming systems are presented. The first is a set of
general purpose subroutines which are designed to simplify the process of
user-program interaction, and to provide a straight-forward means for
handling erroneous input. The input mechanisms are not particularly
sophisticated, but emphasize structure in the user's program. The routines
make range checking so automatic that the programmer has no excuse for
omitting such checks, regardless of how 'quickly' a program is to be
completed. Providing these routines to novice programmers in a classroom
environment has eliminated a lot of frustration over using the scanf()
function.
The second and third programming systems illustrate Runge-Kutta
integration. The Runge-Kutta formalism is a standard numerical technique
for handling the numerical integration of one or more first order ordinary
differential equations. Interested readers may wish to consult the book
Numerical Analysis, by Richard L. Burden et. al. (Prindle, Weber, Schmidt
publishers, Second edition, 1981). This is the source for the algorithms
presented in the code and is also a fine reference for introductory
numerical methods. The choice of Runge-Kutta routines as examples was
based on their widespread use in scientific work.
Acknowledgements
The general purpose library has received extensive use by Caltech
students during the past school year. Thanks are due to Scott Lewicki who
discovered a couple of minor details which caused major errors.
The Runge-Kutta code was developed by Michael J. Roberts and
myself. Mr. Roberts developed the major portion of the RKSYS (multiple
equations) routines as a project for Caltech's Physics 20 course:
Introduction to Computational Physics (Prof. Geoffrey C. Fox, instructor).
Approximately sixty man hours were spent in developing, testing and
debugging this code. Mr. Roberts also wrote the original version of the
documentation for RKSYS included in modified form as Table III.
GPR: General Purpose Routines
The general purpose library consists of five subroutines. Four of
these subroutines deal with input. The fifth is a simple facility for
printing files to the console. This latter routine is called display() and
will be considered separately from the input functions.
The other routines are iinp(), finp(), sinp() and cinq(). The
first three provide integer, floating point, and string input respectively.
The latter is a yes-no question processor. The exact calling sequences for
each of the routines is provided in Table I.
.pa
---------- Table I. ----------
1. int iinp(prompt,cflag,low,high);
char *prompt: optional prompt string to be printed before input
char cflag: checking flag: if non-zero, range checking is performed
int low
int high: low, high are (inclusive) range checking values
iinp() repeats input until a valid number is entered; the valid
number is the function's return value.
2. double finp(prompt,cflag,low,high);
char *prompt: optional prompt string to be printed before input
char cflag: checking flag: if non-zero, range checking is performed
double low
double high: low, high are (inclusive) range checking values
finp() repeats input until a valid number is entered; the valid
number is the function's return value
3. len = sinp(prompt,string,length);
int len: length of entered string
char *prompt: optional prompt string to be printed before input
int length: maximum length of input string
sinp() ignores leading spaces.
4. retn = cinq(prompt);
int retn: 1 --> 'Y' was typed, 0 --> 'N' was typed
char *prompt: optional prompt string to be printed before input
5. retn = display(fname);
int retn: 0 --> success, -1 --> failure
char *fname: null-terminated name of file
display() prints the specified file on the standard output device.
---------- End Table I. ----------
.pa
Traditionally, user input consists of a sequence of lines such as
the following sequence for inputting an integer:
#define MIN 10
#define MAX 100
...
int input;
...
while (1) /* input loop */
{
printf("Enter input variable --> ");
if (scanf("%u",&input) != 1) /* get variable */
{
drain(); /* drain spurious characters */
continue; /* skip range checks */
}
/* do range checking */
if ((input >= MIN) && (input <= MAX))
break; /* we are done */
printf("\nNumber out of range\n");
} /* keep looping until scanf() can read a variable */
Entering this sequence repeatedly can be rather tedious from a
programmatical point of view, so error checking is often omitted. This
practice leads to programs which do not handle user mistakes intelligently.
The GPR routines allow the above sequence to be replaced by a single line
of code:
input = iinp("Enter input variable -->",1,MIN,MAX);
Since using the GPR input functions is easier than using scanf(), this
variety of function should be well-received and can be used in lieu of
scanf() for most purposes. A further advantage of these functions is that
they unclutter the user's program. More sophisticated checking will still
need to be included explicitly: the input functions only do range checking.
The display() function was included to encourage users to provide
on-line help/documentation along with their programs. This function allows
users to print out additional text whenever appropriate. With this
function, a trivial on-line help facility can be created; a help feature is
almost always appropriate but usually is omitted.
The GPR routines may be found in Listing I. The current list is
not exhaustive but intended only to suggest a trend for additional
routines.
RK4: Runge-Kutta Algorithm
We have investigated the general purpose library which simplifies
the task of correct input. Now we want to consider a scientific
application of C: single equation Runge-Kutta integration. Before